package com.styytech.dadabao.widge.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.styytech.dadabao.R;

/**
 * 弹出警告框，确认和取消
 */
public class CustomAlertDialog {
    private Context context;
    public AlertDialog dlg;
    public TextView btn_ok;// 确定按钮
    public TextView btn_cancel;// 取消按钮
    public TextView tv_title;// 标题
    public TextView tv_account;// 账号
    public EditText et_password;// 密码
    public String title;// 标题
    public String message;// 账号
    private View v_spaceline;//分割线

    public CustomAlertDialog() {
        super();
    }

    public CustomAlertDialog(Context context) {
        super();
        this.context = context;
    }

    /**
     * 显示自定义提示对话框
     *
     * @param context - 上下文
     * @param title   - 提示框标题（大大宝账号）
     * @param message - 提示框消息内容(手机号)
     */
    public CustomAlertDialog(Context context, String title, String message) {
        super();
        this.context = context;
        this.title = title;
        this.message = message;
        create();
    }

    private void create() {
        dlg = new AlertDialog.Builder(context).create();
        dlg.show();
        dlg.setContentView(R.layout.custom_alert_dialog);
        //下面两行代码加入后即可弹出输入法
        dlg.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dlg.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        // 找自定义布局的控件
        findView();
    }

    public void show() {

        // 初始化用户设置的对话框 目前是固定写法，后期需求变化时再完善
        initView();
    }

    /**
     * 找自定义布局的控件
     */
    private void findView() {
        btn_ok = (TextView) dlg.findViewById(R.id.tv_ok);
        btn_cancel = (TextView) dlg.findViewById(R.id.tv_cancel);
        tv_title = (TextView) dlg.findViewById(R.id.tv_title);
        tv_account = (TextView) dlg.findViewById(R.id.tv_account);
        et_password = (EditText) dlg.findViewById(R.id.et_password);
        v_spaceline = dlg.findViewById(R.id.v_spaceline);
    }

    /**
     * 初始化用户设置的对话框 目前是固定写法，后期需求变化时再完善
     */
    private void initView() {
        if (!title.isEmpty()) {
            tv_title.setText(title.toString().trim());
        } else {
            tv_title.setVisibility(View.GONE);
        }
        if (!message.isEmpty()) {
            tv_account.setText(message.toString().trim());
        } else {
            tv_account.setVisibility(View.GONE);
        }
        if (!btn_ok.getText().toString().isEmpty()) {
            btn_ok.setVisibility(View.VISIBLE);
        } else {
            v_spaceline.setVisibility(View.GONE);
            btn_ok.setVisibility(View.GONE);
            btn_cancel.setBackgroundResource(R.drawable.shape_radius_bottom);
        }
        if (!btn_cancel.getText().toString().isEmpty()) {
            btn_cancel.setVisibility(View.VISIBLE);
        } else {
            btn_cancel.setVisibility(View.GONE);
            v_spaceline.setVisibility(View.GONE);
            btn_ok.setBackgroundResource(R.drawable.shape_radius_bottom);
        }
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}
